package cn.org.donald.framework.config;

import cn.org.donald.framework.annotation.IntegerLimit;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author : Donald
 * @since : 2020/12/18 11:18
 */
public class IntegerLimitValidate implements ConstraintValidator<IntegerLimit,Integer> {

    private int[] includes;

    @Override
    public void initialize(IntegerLimit constraintAnnotation) {
        this.includes = constraintAnnotation.includes();

    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        if (integer==null){
            return false;
        }
        for (int include : this.includes) {
            if (integer == include) {
                return true;
            }
        }
        return false;
    }
}
